<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>[elasticsearch]Elasticsearch原理和语法 - Never Give Up</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="ZhangKQ" /><meta name="description" content="Elasticsearch原理和语法 一. 介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不" /><meta name="keywords" content="中间件, middleware, elasticsearch" />






<meta name="generator" content="Hugo 0.92.0 with theme even" />


<link rel="canonical" href="https://blog.nevergiveup.tech/post/middleware/elasticsearch%E5%8E%9F%E7%90%86%E5%92%8C%E8%AF%AD%E6%B3%95/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="[elasticsearch]Elasticsearch原理和语法" />
<meta property="og:description" content="Elasticsearch原理和语法 一. 介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://blog.nevergiveup.tech/post/middleware/elasticsearch%E5%8E%9F%E7%90%86%E5%92%8C%E8%AF%AD%E6%B3%95/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-05-12T16:37:56+08:00" />
<meta property="article:modified_time" content="2022-02-10T16:37:56+08:00" />

<meta itemprop="name" content="[elasticsearch]Elasticsearch原理和语法">
<meta itemprop="description" content="Elasticsearch原理和语法 一. 介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不"><meta itemprop="datePublished" content="2018-05-12T16:37:56+08:00" />
<meta itemprop="dateModified" content="2022-02-10T16:37:56+08:00" />
<meta itemprop="wordCount" content="3569">
<meta itemprop="keywords" content="中间件,middleware,elasticsearch," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="[elasticsearch]Elasticsearch原理和语法"/>
<meta name="twitter:description" content="Elasticsearch原理和语法 一. 介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Never Give Up</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/remark/">
        <li class="mobile-menu-item">随言碎语</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Never Give Up</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/remark/">随言碎语</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">[elasticsearch]Elasticsearch原理和语法</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-05-12 </span>
        <div class="post-category">
            <a href="/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/"> 中间件 </a>
            </div>
          <span class="more-meta"> 约 3569 字 </span>
          <span class="more-meta"> 预计阅读 8 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#elasticsearch原理和语法"><strong>Elasticsearch原理和语法</strong></a>
      <ul>
        <li><a href="#一-介绍"><strong>一.</strong> <strong>介绍</strong></a></li>
        <li><a href="#二基本概念"><strong>二.基本概念</strong></a>
          <ul>
            <li><a href="#1什么是elasticsearch">1.什么是Elasticsearch</a></li>
            <li><a href="#2实时性">2.实时性</a></li>
            <li><a href="#3集合">3.集合</a></li>
            <li><a href="#4节点">4.节点</a></li>
            <li><a href="#5索引">5.索引</a></li>
            <li><a href="#6类型">6.类型</a></li>
            <li><a href="#7文档">7.文档</a></li>
            <li><a href="#8mysql和elasticsearch-对照">8.mysql和Elasticsearch 对照</a></li>
            <li><a href="#9分片及副本">9.分片及副本</a></li>
          </ul>
        </li>
        <li><a href="#三-rest-api"><strong>三.</strong> <strong>REST API</strong></a>
          <ul>
            <li><a href="#1一些常用api">1.一些常用API</a></li>
            <li><a href="#2索引管理">2.索引管理</a></li>
            <li><a href="#3文档管理">3.文档管理</a></li>
            <li><a href="#4文档查询">4.文档查询</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h1 id="elasticsearch原理和语法"><strong>Elasticsearch原理和语法</strong></h1>
<h2 id="一-介绍"><strong>一.</strong> <strong>介绍</strong></h2>
<p>Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 Lucene 那么简单，它不仅包括了全文搜索功能，还可以进行以下工作:</p>
<p>分布式实时文件存储，并将每一个字段都编入索引，使其可以被搜索。</p>
<p>实时分析的分布式搜索引擎。</p>
<p>可以扩展到上百台服务器，处理PB级别的结构化或非结构化数据。</p>
<h2 id="二基本概念"><strong>二.基本概念</strong></h2>
<h3 id="1什么是elasticsearch">1.什么是Elasticsearch</h3>
<p>Elasticsearch的文件存储，Elasticsearch是面向文档型数据库，一条数据在这里就是一个文档，用JSON作为文档序列化的格式。</p>
<h3 id="2实时性">2.实时性</h3>
<p>通常延迟1秒</p>
<h3 id="3集合">3.集合</h3>
<p>群集是一个或多个节点（服务器）的集合，这些节点一起保存您的全部数据，并在所有节点之间提供联合索引和搜索功能</p>
<h3 id="4节点">4.节点</h3>
<p>节点是单个服务器，它是群集的一部分，存储您的数据，并参与群集的索引和搜索功能。</p>
<h3 id="5索引">5.索引</h3>
<p>索引是具有相似特征的文档的集合。</p>
<h3 id="6类型">6.类型</h3>
<p>在6.0.0中已弃用。</p>
<h3 id="7文档">7.文档</h3>
<p>文档是可以建立索引的基本信息单位。</p>
<h3 id="8mysql和elasticsearch-对照">8.mysql和Elasticsearch 对照</h3>
<ul>
<li><strong>Index</strong>：Elasticsearch的Index相当于数据库的Table</li>
<li><strong>Type</strong>：这个在新的Elasticsearch版本已经废除（在以前的Elasticsearch版本，一个Index下支持多个Type&ndash;有点类似于消息队列一个topic下多个group的概念）</li>
<li><strong>Document</strong>：Document相当于数据库的一行记录</li>
<li><strong>Field</strong>：相当于数据库的Column的概念</li>
<li><strong>Mapping</strong>：相当于数据库的Schema的概念</li>
<li><strong>DSL</strong>：相当于数据库的SQL（给我们读取Elasticsearch数据的API）</li>
</ul>
<p><img src="file:///C:%5CUsers%5CEDZ%5CAppData%5CLocal%5CTemp%5Cksohtml%5CwpsAD80.tmp.jpg" alt="img"></p>
<h3 id="9分片及副本">9.分片及副本</h3>
<h4 id="1分片">1).分片</h4>
<p>Elasticsearch提供了将索引细分为多个碎片的功能。创建索引时，只需定义所需的分片数量即可。每个分片本身就是一个功能齐全且独立的“索引”，可以托管在群集中的任何节点上。</p>
<p>分片很重要，主要有两个原因：</p>
<p>它允许您水平分割/缩放内容量</p>
<p>它使您可以跨多个分片（可能在多个节点上）分布和并行化操作，从而提高性能/吞吐量</p>
<h4 id="2副本">2).副本</h4>
<p>在随时可能发生故障的网络/云环境中，非常有用，强烈建议您使用故障转移机制，以防碎片/节点因某种原因脱机或消失。为此，Elasticsearch允许您将索引分片的一个或多个副本制作为所谓的副本分片（简称副本）。</p>
<p>复制很重要，主要有两个原因：</p>
<p>如果分片/节点发生故障，它可提供高可用性。因此，重要的是要注意，副本碎片永远不会与从其复制原始/主要碎片的节点分配在同一节点上。</p>
<p>由于可以在所有副本上并行执行搜索，因此它可以扩展搜索量/吞吐量。</p>
<h4 id="3总结">3).总结</h4>
<p>总而言之，每个索引可以分为多个碎片。索引也可以复制零（表示没有副本）或多次。复制后，每个索引将具有主碎片（从中复制的原始碎片）和副本碎片（主碎片的副本）。可以在创建索引时为每个索引定义分片和副本的数量。创建索引后，您可以随时动态更改副本数，但不能事后更改分片数。</p>
<p>默认情况下，Elasticsearch中的每个索引都分配有5个主分片和1个副本，这意味着如果集群中至少有两个节点，则索引将具有5个主分片和另外5个副本分片（1个完整副本），总共每个索引10个分片。</p>
<h2 id="三-rest-api"><strong>三.</strong> <strong>REST API</strong></h2>
<h3 id="1一些常用api">1.一些常用API</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">#查看集群健康状况

GET /_cat/health?v


#查看节点信息

GET /_cat/nodes?v


#查看索引信息

GET /_cat/indices?v
</code></pre></td></tr></table>
</div>
</div><h3 id="2索引管理">2.索引管理</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">#创建索引(加pretty友好显示)

PUT /索引名称?pretty

PUT /school?pretty

 

#删除索引

DELETE /索引名称?pretty

DELETE /school?pretty
</code></pre></td></tr></table>
</div>
</div><h3 id="3文档管理">3.文档管理</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">#索引文档(包含id)(新增或替换)

PUT /索引名称/文档名称/文档id?pretty

body内容(json)

PUT /school/teacher/1?pretty

{

    &#34;id&#34;: 1,

    &#34;name&#34;: &#34;John Doe&#34;

}

 

#索引文档(不包含id)(新增)

POST /索引名称/文档名称?pretty

body内容(json)

POST /school/teacher?pretty

{

  &#34;name&#34;: &#34;Jane Doe&#34;

}

 

#获取文档

GET /索引名称/文档名称/文档id?pretty

GET /school/teacher/1?pretty

 

#更新文档

POST /索引名称/文档名称/文档id/_update?pretty

body内容(json)

POST /school/teacher/1/_update?pretty

{

  &#34;doc&#34;: {

        &#34;id&#34;: 1,

        &#34;name&#34;: &#34;John Doe&#34;,

        &#34;age&#34;: &#34;26&#34;

    }

}

 

#更新文档(使用script)

POST /索引名称/文档名称/文档id/_update?pretty

{

  &#34;script&#34; : js

}

POST /school/teacher/1/_update?pretty

{

  &#34;script&#34; : &#34;ctx._source.age += 5&#34;

}

 

#删除文档

DELETE /索引名称/文档名称/文档id?pretty

DELETE /school/teacher/1?pretty

 

#批量处理文档

POST /索引名称/文档名称/文_bulk?pretty

json串

POST /school/teacher/_bulk?pretty

{&#34;index&#34;:{&#34;_id&#34;:&#34;2&#34;}}

{&#34;name&#34;: &#34;John Doe&#34; }

{&#34;index&#34;:{&#34;_id&#34;:&#34;3&#34;}}

{&#34;name&#34;: &#34;Jane Doe&#34; }

{&#34;update&#34;:{&#34;_id&#34;:&#34;2&#34;}}

{&#34;doc&#34;: { &#34;name&#34;: &#34;John Doe becomes Jane Doe&#34; } }

{&#34;delete&#34;:{&#34;_id&#34;:&#34;3&#34;}}
</code></pre></td></tr></table>
</div>
</div><h3 id="4文档查询">4.文档查询</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span><span class="lnt">118
</span><span class="lnt">119
</span><span class="lnt">120
</span><span class="lnt">121
</span><span class="lnt">122
</span><span class="lnt">123
</span><span class="lnt">124
</span><span class="lnt">125
</span><span class="lnt">126
</span><span class="lnt">127
</span><span class="lnt">128
</span><span class="lnt">129
</span><span class="lnt">130
</span><span class="lnt">131
</span><span class="lnt">132
</span><span class="lnt">133
</span><span class="lnt">134
</span><span class="lnt">135
</span><span class="lnt">136
</span><span class="lnt">137
</span><span class="lnt">138
</span><span class="lnt">139
</span><span class="lnt">140
</span><span class="lnt">141
</span><span class="lnt">142
</span><span class="lnt">143
</span><span class="lnt">144
</span><span class="lnt">145
</span><span class="lnt">146
</span><span class="lnt">147
</span><span class="lnt">148
</span><span class="lnt">149
</span><span class="lnt">150
</span><span class="lnt">151
</span><span class="lnt">152
</span><span class="lnt">153
</span><span class="lnt">154
</span><span class="lnt">155
</span><span class="lnt">156
</span><span class="lnt">157
</span><span class="lnt">158
</span><span class="lnt">159
</span><span class="lnt">160
</span><span class="lnt">161
</span><span class="lnt">162
</span><span class="lnt">163
</span><span class="lnt">164
</span><span class="lnt">165
</span><span class="lnt">166
</span><span class="lnt">167
</span><span class="lnt">168
</span><span class="lnt">169
</span><span class="lnt">170
</span><span class="lnt">171
</span><span class="lnt">172
</span><span class="lnt">173
</span><span class="lnt">174
</span><span class="lnt">175
</span><span class="lnt">176
</span><span class="lnt">177
</span><span class="lnt">178
</span><span class="lnt">179
</span><span class="lnt">180
</span><span class="lnt">181
</span><span class="lnt">182
</span><span class="lnt">183
</span><span class="lnt">184
</span><span class="lnt">185
</span><span class="lnt">186
</span><span class="lnt">187
</span><span class="lnt">188
</span><span class="lnt">189
</span><span class="lnt">190
</span><span class="lnt">191
</span><span class="lnt">192
</span><span class="lnt">193
</span><span class="lnt">194
</span><span class="lnt">195
</span><span class="lnt">196
</span><span class="lnt">197
</span><span class="lnt">198
</span><span class="lnt">199
</span><span class="lnt">200
</span><span class="lnt">201
</span><span class="lnt">202
</span><span class="lnt">203
</span><span class="lnt">204
</span><span class="lnt">205
</span><span class="lnt">206
</span><span class="lnt">207
</span><span class="lnt">208
</span><span class="lnt">209
</span><span class="lnt">210
</span><span class="lnt">211
</span><span class="lnt">212
</span><span class="lnt">213
</span><span class="lnt">214
</span><span class="lnt">215
</span><span class="lnt">216
</span><span class="lnt">217
</span><span class="lnt">218
</span><span class="lnt">219
</span><span class="lnt">220
</span><span class="lnt">221
</span><span class="lnt">222
</span><span class="lnt">223
</span><span class="lnt">224
</span><span class="lnt">225
</span><span class="lnt">226
</span><span class="lnt">227
</span><span class="lnt">228
</span><span class="lnt">229
</span><span class="lnt">230
</span><span class="lnt">231
</span><span class="lnt">232
</span><span class="lnt">233
</span><span class="lnt">234
</span><span class="lnt">235
</span><span class="lnt">236
</span><span class="lnt">237
</span><span class="lnt">238
</span><span class="lnt">239
</span><span class="lnt">240
</span><span class="lnt">241
</span><span class="lnt">242
</span><span class="lnt">243
</span><span class="lnt">244
</span><span class="lnt">245
</span><span class="lnt">246
</span><span class="lnt">247
</span><span class="lnt">248
</span><span class="lnt">249
</span><span class="lnt">250
</span><span class="lnt">251
</span><span class="lnt">252
</span><span class="lnt">253
</span><span class="lnt">254
</span><span class="lnt">255
</span><span class="lnt">256
</span><span class="lnt">257
</span><span class="lnt">258
</span><span class="lnt">259
</span><span class="lnt">260
</span><span class="lnt">261
</span><span class="lnt">262
</span><span class="lnt">263
</span><span class="lnt">264
</span><span class="lnt">265
</span><span class="lnt">266
</span><span class="lnt">267
</span><span class="lnt">268
</span><span class="lnt">269
</span><span class="lnt">270
</span><span class="lnt">271
</span><span class="lnt">272
</span><span class="lnt">273
</span><span class="lnt">274
</span><span class="lnt">275
</span><span class="lnt">276
</span><span class="lnt">277
</span><span class="lnt">278
</span><span class="lnt">279
</span><span class="lnt">280
</span><span class="lnt">281
</span><span class="lnt">282
</span><span class="lnt">283
</span><span class="lnt">284
</span><span class="lnt">285
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">#新建索引

PUT /school?pretty

{

    &#34;settings&#34;:{

        &#34;analysis&#34;:{

            &#34;analyzer&#34;:{

                &#34;ik&#34;:{

                    &#34;tokenizer&#34;:&#34;ik_smart&#34;

                }

            }

        }

    },

    &#34;mappings&#34;:{

        &#34;_doc&#34;:{

            &#34;properties&#34;:{

                &#34;id&#34;:{

                    &#34;type&#34;:&#34;long&#34;

                },

                &#34;name&#34;:{

                    &#34;type&#34;:&#34;text&#34;,

                    &#34;analyzer&#34;:&#34;ik_smart&#34;

                },

                &#34;age&#34;:{

                    &#34;type&#34;:&#34;long&#34;

                },

                &#34;class&#34;:{

                    &#34;type&#34;:&#34;keyword&#34;

                },

                &#34;create_at&#34;:{

                    &#34;type&#34;:&#34;date&#34;

                }

            }

        }

    }

}

 

 

#新建实例文档

POST /school/teacher/_bulk?pretty

{&#34;index&#34;:{&#34;_id&#34;:&#34;1&#34;}}

{&#34;id&#34;: 1,&#34;name&#34;: &#34;John Doe1&#34;,&#34;age&#34;: 21,&#34;class&#34;:&#34;一班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;2&#34;}}

{&#34;id&#34;: 2,&#34;name&#34;: &#34;John Doe2 Doe3&#34;,&#34;age&#34;: 22,&#34;class&#34;:&#34;一班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;3&#34;}}

{&#34;id&#34;: 3,&#34;name&#34;: &#34;John Doe3 Doe4&#34;,&#34;age&#34;: 23,&#34;class&#34;:&#34;一班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;4&#34;}}

{&#34;id&#34;: 4,&#34;name&#34;: &#34;John Doe4&#34;,&#34;age&#34;: 24,&#34;class&#34;:&#34;一班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;5&#34;}}

{&#34;id&#34;: 5,&#34;name&#34;: &#34;John Doe5&#34;,&#34;age&#34;: 25,&#34;class&#34;:&#34;一班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;6&#34;}}

{&#34;id&#34;: 6,&#34;name&#34;: &#34;John Doe6&#34;,&#34;age&#34;: 26,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;7&#34;}}

{&#34;id&#34;: 7,&#34;name&#34;: &#34;John Doe7&#34;,&#34;age&#34;: 27,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;8&#34;}}

{&#34;id&#34;: 8,&#34;name&#34;: &#34;John Doe8&#34;,&#34;age&#34;: 28,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;9&#34;}}

{&#34;id&#34;: 9,&#34;name&#34;: &#34;John Doe9&#34;,&#34;age&#34;: 29,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;10&#34;}}

{&#34;id&#34;: 10,&#34;name&#34;: &#34;John Doe10&#34;,&#34;age&#34;: 30,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;11&#34;}}

{&#34;id&#34;: 11,&#34;name&#34;: &#34;John Doe11&#34;,&#34;age&#34;: 22,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

{&#34;index&#34;:{&#34;_id&#34;:&#34;12&#34;}}

{&#34;id&#34;: 12,&#34;name&#34;: &#34;John Doe12&#34;,&#34;age&#34;: 22,&#34;class&#34;:&#34;二班&#34;,&#34;create_at&#34;:&#34;2021-05-15T16:19:13&#34;}

 

 

#搜索

GET /school/_search?q=*&amp;sort=id:asc&amp;pretty

GET /school/_search

{

    //&#34;query&#34;:

    //{

      //&#34;match_all&#34;: {}//匹配全部

      //&#34;match&#34; : {&#34;age&#34; : 21}

      //&#34;match&#34; : {&#34;name&#34; : &#34;Doe3 Doe4 }

      //&#34;match_phrase&#34; : {&#34;name&#34; : &#34;Doe3 Doe4&#34;}

      //&#34;bool&#34; : {

      //  &#34;must&#34; : [

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe3&#34;}},

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe4&#34;}}

      //  ]

      //}

      //&#34;bool&#34; : {

      //  &#34;should&#34; : [

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe3&#34;}},

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe4&#34;}}

      //  ]

      //}

      //&#34;bool&#34; : {

      //  &#34;must_not&#34;: [

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe3&#34;}},

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe4&#34;}}

      //  ]

      //}

      //&#34;bool&#34; : {

      //  &#34;must&#34;: [

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe3&#34;}}

      //  ],

      //  &#34;must_not&#34;: [

      //    {&#34;match&#34; : {&#34;name&#34; : &#34;Doe4&#34;}}

      //  ]

      //}

      //&#34;bool&#34; : {

      //  &#34;must&#34; : [

      //    {&#34;term&#34; : {&#34;age&#34; : {&#34;value&#34; : 21}}}},

      //    {&#34;term&#34; : {&#34;name&#34; : {&#34;value&#34; : &#34;Doe3&#34;}}}

      //  ]

      //}

      //&#34;bool&#34; : {

      //  &#34;must&#34;: {&#34;match_all&#34; : {}},

      //  &#34;filter&#34;: {

      //    &#34;range&#34;: {

      //      &#34;age&#34;: {

      //        &#34;gte&#34;: 21,

      //        &#34;lte&#34;: 30

      //      }

      //    }

      //  }

      //}

    //},

    //&#34;_source&#34;: [&#34;id&#34;, &#34;name&#34;, &#34;age&#34;],

    //&#34;from&#34; : 0, //从0开始

    //&#34;size&#34; : 3,

    &#34;size&#34; : 0,

    &#34;aggs&#34;: {

      &#34;group_by_class&#34;: {

        &#34;terms&#34;: {

          &#34;field&#34;: &#34;class.keyword&#34;

        },

        &#34;aggs&#34;: {

          &#34;average_age&#34;: {

            &#34;avg&#34;: {

              &#34;field&#34;: &#34;age&#34;

            }

          }

        }

      }

    },

    &#34;highlight&#34; : {&#34;fields&#34;:{&#34;name&#34;:{}}}

    //&#34;sort&#34;: [

    //  { &#34;id&#34;: &#34;asc&#34; }

    //  { &#34;id&#34;: {&#34;order&#34;:&#34;asc&#34;} }

    //]

}
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">ZhangKQ</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-02-10
        
    </span>
  </p>
  
  
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/wechat-qr-code.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/alipay-qr-code.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a>
          <a href="/tags/middleware/">middleware</a>
          <a href="/tags/elasticsearch/">elasticsearch</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/middleware/elasticsearch%E5%9C%A8linux%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">[elasticsearch]Elasticsearch在linux安装及使用</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/middleware/rabbitmq%E5%9C%A8docker%E5%AE%89%E8%A3%85%E5%8F%8A%E4%BD%BF%E7%94%A8/">
            <span class="next-text nav-default">[rabbitmq]RabbitMQ在Docker安装及使用</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wdyxzkq@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/dysoso" class="iconfont icon-github" title="github"></a>
      <a href="https://gitee.com/dysoso" class="iconfont icon-gitlab" title="gitlab"></a>
  <a href="https://blog.nevergiveup.tech/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://blog.nevergiveup.tech/">blog.nevergiveup.tech</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2021 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span><a href="https://beian.miit.gov.cn/">蜀ICP备2021005948号-1</a></span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'never-give-up', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
